package cn.good.user.controller;

import cn.good.dto.Dto;
import cn.good.dto.DtoUtil;
import cn.good.user.service.QGUserService;
import cn.good.utils.Constant;
import cn.good.vo.UserVO;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.jms.Queue;

/**
 * 实现用户注册
 */
@Api(value="/api", tags="用户注册模块")
@RestController
@RequestMapping("/api")
public class RegisterController {

    @Resource
    private QGUserService qgUserService;
    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;

    private Queue queue;

    /**
     * 手机注册
     * @param userVO
     * @return
     */
    @ApiOperation(value="手机注册功能API",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用手机注册")
    @RequestMapping(value = "/registerbyphone",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto doregisterByPhone(@ApiParam(name="itripUserVO",value="用户信息",required=true)@RequestBody UserVO userVO) {
        if(userVO.getUserCode()==null||!userVO.getUserCode().matches(Constant.PHONE_RULE)){
            return DtoUtil.returnFail("手机号码不符合规则！","100011");
        }
        try {
            //验证用户
            qgUserService.userExist(userVO.getUserCode());
            queue = new ActiveMQQueue(Constant.MQ_PHONE);
            jmsMessagingTemplate.convertAndSend(queue, JSON.toJSONString(userVO));
            queue = new ActiveMQQueue(Constant.MQ_PHONE_JSM);
            jmsMessagingTemplate.convertAndSend(queue,JSON.toJSONString(userVO));
            //保存用户
//            qgUserService.saveUser(userVO);
            // 发送短信
//            qgUserService.sendSmsCode(userVO.getUserCode());
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("短信发送失败！","100012");
        }
        return DtoUtil.returnSuccess("发送短信成功!");
    }

    /**
     * 手机短信验证
     * @param user
     * @param code
     * @return
     */
    @ApiOperation(value="手机短信验证API",httpMethod = "PUT", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用手机短信验证")
    @RequestMapping(value = "/validate",method = RequestMethod.PUT,produces = "application/json")
    @ResponseBody
    public Dto dovalidateSms(@ApiParam(name="user",value="用户名",required=true)@RequestParam String user,@ApiParam(name="code",value="短信验证码",required=true)@RequestParam String code) {
        if(!user.matches(Constant.PHONE_RULE)){
            DtoUtil.returnFail("用户名不符合规则！","100015");
        }
        try {
            if(qgUserService.phoneActivate(user,code)){
                return DtoUtil.returnFail("激活失败","100017");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("激活失败！？","100016");
        }
        return DtoUtil.returnSuccess("激活成功！！");
    }
}
